<?php
/**
 * 积分商品订单管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Order;

use App\Admin\Actions\Order\ExchangeDeliverGoods;
use App\Admin\Exporter\Order\OrderExpoter;
use App\Http\Controllers\Controller;
use App\Models\DataDictionaryModel;
use App\Models\yb\PointsGoodsModel;
use App\Models\yb\ExchangeGoodsOrderModel;
use App\Models\yb\PointsGoodsTypeModel;
use App\Models\yb\UserAddressModel;
use App\Models\yb\UserModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;



class PointsOrderController extends Controller
{
    use ModelForm;

    protected $header = '积分商品订单';

    /**
     * 工作状态列表
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $order_data = ExchangeGoodsOrderModel::where(['id'=>$id])->first();
            if($order_data['status'] == 0){
                $order_data['status'] = "待发货";
            }elseif($order_data['status'] == 1){
                $order_data['status'] = "已发货";
            }elseif($order_data['status'] == 2){
                $order_data['status'] = "确认发货";
            }
            $user_data = UserModel::where(['id'=>$order_data['user_id']])->first();
            $user_address_data = UserAddressModel::where(['id'=>$order_data['address_id']])->first();
            $goods_data = PointsGoodsModel::where(['id'=>$order_data['goods_id']])->first();
            $goods_data['goods_cover'] = env('OSS_URL').$goods_data['goods_cover'];
            $goods_data['goods_type_name'] = PointsGoodsTypeModel::selectValue('id',$goods_data['goods_points_type']);
 
            $content->header($this->header);
            $content->description('详情');
            $content->body(view('admin.order.points_detail',[
                'user_data' => $user_data,
                'order_data' => $order_data,
                'user_address_data' => $user_address_data,
                'goods_data' => $goods_data
            ]));

        });
    }

    public function grid(){

        $grid = new Grid(new ExchangeGoodsOrderModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('order_sn','订单编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.nickname','下单人昵称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('User.phone','下单手机号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('amount','支付积分')->display(function($filed){
            return empty($filed) ?  config('view.EmptyData') : "¥".$filed;
        });

        $grid->column('Goods.goods_title', '商品详情')->display(function($filed){
            return empty($filed) ? '暂无' : $filed;
        })->modal('商品详情', function($model){
            $goods_data = PointsGoodsModel::where(['id'=>$this->goods_id])->select('id','goods_points_type','goods_title','goods_price','goods_specs','status')->get();
            $goods_data = $goods_data->toArray();
            $goods_data[0]['goods_points_type'] = PointsGoodsTypeModel::selectValue('id',$goods_data[0]['goods_points_type']);
            if($goods_data[0]['status']==0){
                $goods_data[0]['status']="禁用";
            }else{
                $goods_data[0]['status']="启用";
            }
            unset($goods_data[0]['action_id']);
            $goods_data[0]['number'] = $this->number;
            return new Table(['编号','类型','标题','单价','规格','状态','下单数量'], $goods_data);
        });

        $grid->column('status','订单状态')->using([
            '0'=>'待发货',
            '1'=>'已发货',
            '2'=>'确认发货',
        ], '未知')->dot([
            0 => 'danger',
            1 => 'info',
            2 => 'warning',
        ], 'success');

        $grid->column('created_at','创建时间')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        })->sortable();
        
        $grid->column('id','发货')->display(function ($id, $column) use ($grid) {

            if($this->status == 0){
                $action = new ExchangeDeliverGoods();
                return $action
                    ->asColumn()
                    ->setGrid($grid)
                    ->setColumn($column)
                    ->setRow($this);
            }

        });
        

        //搜索
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->like('Order.goods_title','商品名称');
                $filter->between('created_at', '下单时间')->datetime();
            });
            $filter->column(1/2, function ($filter) {
                $filter->like('User.nickname','用户名称');
                $filter->equal('status', '状态')->select([
                    '0'=>'待发货',
                    '1'=>'已发货',
                    '2'=>'确认发货',
                ]);
            });
            $filter->disableIdFilter();
        });

        $grid->fixColumns(1, -1);

        $grid->exporter(new OrderExpoter());

        $grid->actions(function($actions){
            $actions->disableDelete();
            $actions->disableEdit();
        });

        $grid->disableExport();

        $grid->disableCreateButton();

        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new GoodsModel());

        $form->text('goods_title', '商品标题')->rules('required|max:150');

        $form->file('goods_cover','封面图')->uniqueName()->removable();

        $form->select('goods_type', '类型')->options(GoodsTypeModel::selectOptions())->rules('required');

        $form->text('goods_specs', '规格')->rules('required|max:150');

        $form->currency('goods_price', '单价')->symbol('￥');

        $form->number('goods_stock', '库存');

        $form->radio('status', '状态')->options(DataDictionaryModel::GoodsStatus)->default('0');

        $form->editor('goods_detail','详情');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });

        return $form;
    }



}